package utils

import (
	"strings"
)

func CleanWhitespace(s string) string {
	// strings.Fields 会按空白字符分割字符串，然后自动忽略掉所有空项
	// strings.Join 会用单个空格把它们重新组合起来
	return strings.Join(strings.Fields(s), " ")
}

func CleanAIResponse(rawContent string) string {
	// 1. 替换所有全角引号为半角引号
	cleanedContent := strings.ReplaceAll(rawContent, "“", "\"")
	cleanedContent = strings.ReplaceAll(cleanedContent, "”", "\"")

	return cleanedContent
}

func CleanAndFixJSON(input string) string {
	input = strings.TrimSpace(input)
	input = strings.TrimPrefix(input, "```json")
	input = strings.TrimPrefix(input, "```")
	input = strings.TrimSuffix(input, "```")
	input = strings.TrimSpace(input)
	// 去除 BOM
	input = strings.TrimPrefix(input, "\uFEFF")

	// 去除零宽空格
	input = strings.ReplaceAll(input, "\u200B", "")
	input = strings.ReplaceAll(input, "\u200C", "")
	input = strings.ReplaceAll(input, "\u200D", "")

	input = strings.ReplaceAll(input, "“", "\"")
	input = strings.ReplaceAll(input, "”", "\"")

	return input
}
